import { dialogForm, DialogFormOptions } from "@/utils/dialogForm";

import { getListAllUser } from "@/api/modules/sys";
import { getProductModelList } from "@/api/modules/carType";
import { addProjectProblem } from "@/api/modules/project"; // getProjectPage,
import { getProjectListCodeAll } from "@/api/modules/project";

import { useDict } from "@/hooks/useDict";
import { QA } from "@/constants";
import { ref } from "vue";
export const useAdd = (options: any) => {
  const { tableRef } = options;
  const { promise, getDictId } = useDict([
    "projectProblemType",
    "projectProblemStatus",
    "projectProblemStage",
    "projectProblemPosition"
  ]);

  const config = ref({});
  const listAllUser = ref<any>([]);
  const dictList = ref<any>([]);
  const agvTypeList = ref<any>([]);
  const projectList: any = ref([]);
  const openDialog = async () => {
    await getDataInit();
    config.value = {
      title: "新增问题跟踪",
      width: 80,
      height: "80vh",
      form: {
        formData: {
          projectId: undefined,
          projectName: undefined,
          projectCode: undefined,
          typeId: undefined,
          productCategoryId: undefined,
          productId: undefined,
          name: undefined,
          content: undefined,
          problemSite: undefined,
          problemTime: undefined,
          problemUserId: undefined,
          problemUserName: undefined,
          problemStageId: undefined,
          problemPartId: undefined,
          problemReason: undefined,
          problemCycle: undefined,
          replyContent: undefined,
          replyUserId: undefined,
          replyUserName: undefined,
          planCompleteTime: undefined,
          completeTime: undefined,
          convertQuality: undefined,
          problemFollow: undefined,
          status: undefined,
          projectProblemImgList: []
        },
        rules: {},
        attribute: {
          "label-suffix": ""
        }
      },
      api: addProjectProblem,
      column: [
        {
          type: "group",
          title: "基本信息",
          direction: "",
          len: 4,
          layout: "grid",
          list: [
            {
              type: "select",
              key: "projectId",
              formItemAttribute: {
                label: "订单编号"
              },
              attributes: {
                placeholder: "请选择",
                options: projectList.value,
                multiple: false,
                filterable: true,
                clearable: true,

                changeFn: (val: any, originConfig: any) => {
                  const country = projectList.value.find((item: any) => item.id === val);
                  originConfig.formData.projectCode = country?.code;
                  originConfig.formData.projectName = country?.name;
                }
              },
              fieldNames: {
                label: "nameClone",
                value: "id"
              }
            },
            {
              type: "select",
              key: "typeId",
              formItemAttribute: {
                label: "问题类型"
              },
              attributes: {
                placeholder: "请填写",
                options: dictList.value[getDictId("projectProblemType")],
                multiple: false,
                filterable: true,
                clearable: true
              }
            },
            {
              type: "select",
              key: "status",
              formItemAttribute: {
                label: "进度"
              },
              attributes: {
                placeholder: "请选择",
                options: dictList.value[getDictId("projectProblemStatus")],
                multiple: false,
                filterable: true,
                clearable: true
              }
            },
            {
              type: "select",
              key: "productCategoryId",
              formItemAttribute: {
                label: "车型"
              },
              attributes: {
                placeholder: "请选择",
                options: agvTypeList.value,
                multiple: false,
                filterable: true,
                clearable: true
              }
            },
            {
              type: "input",
              key: "productCategoryName",
              formItemAttribute: {
                label: "车型名称"
              },
              attributes: {
                placeholder: "请填写",
                clearable: true
              }
            },

            {
              type: "input",
              key: "problemSite",
              formItemAttribute: {
                label: "现场地点"
              },
              attributes: {
                placeholder: "请填写"
              }
            },
            {
              type: "select",
              key: "userId",
              formItemAttribute: {
                label: "项目经理"
              },
              attributes: {
                placeholder: "请选择",
                options: listAllUser.value,
                multiple: false,
                filterable: true,
                clearable: true,
                changeFn: (val: any, originConfig: any) => {
                  const country = listAllUser.value.find((item: any) => item.id === val);
                  originConfig.formData.userName = country?.name;
                }
              }
            }
          ]
        },
        {
          type: "group",
          title: "问题描述",
          direction: "",
          len: 4,
          layout: "grid",
          list: [
            {
              type: "select",
              key: "problemPartId",
              formItemAttribute: {
                label: "发生位置"
              },
              attributes: {
                placeholder: "请填写",
                options: dictList.value[getDictId("projectProblemPosition")],
                multiple: false,
                filterable: true,
                clearable: true
              }
            },
            {
              type: "select",
              key: "problemStageId",
              formItemAttribute: {
                label: "发生阶段"
              },
              attributes: {
                placeholder: "请填写",
                options: dictList.value[getDictId("projectProblemStage")],
                multiple: false,
                filterable: true,
                clearable: true
              }
            },
            {
              type: "select",
              key: "problemUserId",
              formItemAttribute: {
                label: "发现人员"
              },
              attributes: {
                placeholder: "请选择",
                options: listAllUser.value,
                multiple: false,
                filterable: true,
                clearable: true,
                changeFn: (val: any, originConfig: any) => {
                  const country = listAllUser.value.find((item: any) => item.id === val);
                  originConfig.formData.problemUserName = country?.name;
                }
              }
            }
          ]
        },
        {
          type: "input",
          key: "content",
          formItemAttribute: {
            label: "问题描述"
          },
          attributes: {
            placeholder: "请填写",
            type: "textarea",
            rows: 8
          }
        },
        {
          type: "file",
          key: "projectProblemImgList",
          formItemAttribute: {
            label: "问题图片"
          },
          attributes: {
            "prefix-data": QA,
            multiple: true,
            showFileList: true,
            limit: 30,
            width: "240px",
            height: "240px",
            type: "image",
            listType: "picture-card",
            fileType: ["image/jpeg", "image/png", "image/gif"]
          }
        },
        {
          type: "input",
          key: "problemReason",
          formItemAttribute: {
            label: "问题原因"
          },
          attributes: {
            placeholder: "请填写",
            type: "textarea",
            rows: 8
          }
        },
        {
          type: "group",
          title: "问题对策",
          direction: "",
          len: 4,
          layout: "grid",
          list: [
            {
              type: "select",
              key: "replyUserId",
              formItemAttribute: {
                label: "对策人员"
              },
              attributes: {
                placeholder: "请选择",
                options: listAllUser.value,
                multiple: false,
                filterable: true,
                clearable: true,
                changeFn: (val: any, originConfig: any) => {
                  const country = listAllUser.value.find((item: any) => item.id === val);
                  originConfig.formData.replyUserName = country?.name;
                }
              }
            },
            {
              type: "date-picker",
              key: "planCompleteTime",
              formItemAttribute: {
                label: "计划完成"
              },
              attributes: {
                type: "date",
                placeholder: "请选择",
                format: "YYYY-MM-DD HH:mm:ss",
                valueFormat: "YYYY-MM-DD HH:mm:ss"
              }
            },
            {
              type: "date-picker",
              key: "completeTime",
              formItemAttribute: {
                label: "实际完成"
              },
              attributes: {
                type: "date",
                placeholder: "请选择",
                format: "YYYY-MM-DD HH:mm:ss",
                valueFormat: "YYYY-MM-DD HH:mm:ss"
              }
            }
          ]
        },
        {
          type: "input",
          key: "problemFollow",
          formItemAttribute: {
            label: "进展跟踪"
          },
          attributes: {
            placeholder: "请填写",
            type: "textarea",
            rows: 8
          }
        }
      ],
      transformData: (data: any) => {
        data.projectProblemImgList = data.projectProblemImgList.map((item: any) => {
          return {
            name: item.name,
            path: item.path
          };
        });
        return data;
      },
      message: "问题创建成功"
    };

    const options: DialogFormOptions = {
      config: config.value,
      closeCallback: () => {},
      successCallback: () => {
        tableRef.value.refresh();
      }
    };

    dialogForm(options);
  };
  // 获取全部用户
  const getListAllUserFn = async () => {
    const { data } = await getListAllUser();
    listAllUser.value = data;
  };
  //  获取所有车型

  const getCarType = async () => {
    const { data } = await getProductModelList();
    agvTypeList.value = data || [];
  };
  // 获取所有项目

  const getProject = async () => {
    const { data } = await getProjectListCodeAll();
    projectList.value =
      (data &&
        data.map((item: any) => {
          item.nameClone = item.code + "-" + item.name;
          return item;
        })) ||
      [];
  };

  // 数据获取
  const getDataInit = async () => {
    await Promise.all([getListAllUserFn(), getCarType(), getProject()]);
    dictList.value = await promise;
  };
  return {
    openDialog
  };
};
